//
//  AXFDeliverAddressController.m
//  AXFEightGroup
//
//  Created by Haozhong Chen on 2016/12/22.
//  Copyright © 2016年 ya. All rights reserved.
//

#import "AXFDeliverAddressController.h"
#import "AXFDataModel.h"
#import "AXFSelectDeliverAddressCell.h"
#import "AXFDetailAddressController.h"
#import "AXFLocationManager.h"


static NSString*SelectDeliverAddressCellID=@"SelectDeliverAddressCellID";

@interface AXFDeliverAddressController ()<UIGestureRecognizerDelegate,UITableViewDelegate,UITableViewDataSource,passDeliverAddressDataDelegate>


@property(nonatomic,strong)NSArray<AXFDataModel*>*deliverAddressArray;
@property(nonatomic,weak)UITableView*tableView;
@property(nonatomic,strong)NSIndexPath*indexPath;

@end

@implementation AXFDeliverAddressController

- (void)viewDidLoad {
    
//    self.view.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    [super viewDidLoad];
    [self loadData];
    [self setupUI];

}

-(void)setupUI
{
    self.title=@"新增地址";
    self.view.backgroundColor=[UIColor lightGrayColor];

    
    
    
    UITableView*tableView=[[UITableView alloc]initWithFrame:self.view.bounds];
    tableView.delegate=self;
    tableView.dataSource=self;
    self.tableView=tableView;
    [self.view addSubview:tableView];

    
    [tableView registerNib:[UINib nibWithNibName:@"AXFSelectDeliverAddressCell" bundle:nil] forCellReuseIdentifier:SelectDeliverAddressCellID];
    tableView.estimatedRowHeight=100;
    tableView.rowHeight=UITableViewAutomaticDimension;
    tableView.tableFooterView=[[UIView alloc]init];
    
    
    UIView*addAddressView=[[UIView alloc]init];
    addAddressView.backgroundColor=[UIColor colorWithRed:1.00 green:0.85 blue:0.00 alpha:1.00];
    [self.view addSubview:addAddressView];
    [addAddressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(44);
        make.width.mas_equalTo(SCREEN_WIDTH*0.8);
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view);
    }];
    
    
    UIButton*addAddressButton=[[UIButton alloc]init];
    [addAddressButton setTitle:@"新增地址" forState:UIControlStateNormal];
    [addAddressButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    addAddressButton.titleLabel.font=[UIFont systemFontOfSize:12];
    [addAddressButton sizeToFit];
    [addAddressView addSubview:addAddressButton];
    [addAddressButton addTarget:self action:@selector(clickedAddAddressButton:) forControlEvents:UIControlEventTouchUpInside];
    [addAddressButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(addAddressView);
    }];
    
    
    
    
    
//    //创建
//    UIView *bottomView = [[UIView alloc] init];
//    bottomView.backgroundColor = [UIColor whiteColor];
//    [self.view addSubview:bottomView];
//    
//    UIButton *addAddressButton = [[UIButton alloc] init];
//    [addAddressButton setBackgroundImage:[UIImage imageNamed:@"v2_coupon_verify_normal"] forState:UIControlStateNormal];
//    [addAddressButton setTitle:@"+新增地址" forState:UIControlStateNormal];
//    [addAddressButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
//    [addAddressButton addTarget:self action:@selector(clickedAddAddressButton:) forControlEvents:UIControlEventTouchUpInside];
//    [bottomView addSubview:addAddressButton];
//    
//    //约束
//    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.height.mas_equalTo(60);
//        make.left.bottom.right.mas_equalTo(self.view);
//    }];
//    
//    [addAddressButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.center.equalTo(bottomView);
//        make.width.mas_equalTo(250);
//        make.height.mas_equalTo(40);
//    }];
//    
//
//    
    
    
    
    
    //返回按钮
    UIButton*backButton=[[UIButton alloc]initWithFrame:CGRectZero];
    [backButton setImage:[UIImage imageNamed:@"v2_goback"] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:@"v2_goback"] forState:UIControlStateSelected];
    [backButton sizeToFit];
    [backButton addTarget:self action:@selector(clickedBackButton) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationController.interactivePopGestureRecognizer.delegate=self;
    
}


-(void)clickedBackButton
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)clickedAddAddressButton:(UIButton*)button
{
    UIStoryboard*stroyboard=[UIStoryboard storyboardWithName:@"AXFDetailAddressController" bundle:nil];
    
    AXFDetailAddressController*vc=[stroyboard instantiateViewControllerWithIdentifier:@"AddAddress"];
    vc.dataModel=[AXFDataModel new];
    vc.delegate=self;
    _indexPath=nil;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)clickedEditAddressButton:(NSIndexPath*)indexPath
{
    UIStoryboard*stroyboard=[UIStoryboard storyboardWithName:@"AXFDetailAddressController" bundle:nil];
    
    AXFDetailAddressController*vc=[stroyboard instantiateViewControllerWithIdentifier:@"EditAddress"];
    vc.dataModel=_deliverAddressArray[indexPath.row];
    vc.delegate=self;
    _indexPath=indexPath;
    
    [self.navigationController pushViewController:vc animated:YES];
}


-(void)addAddressData:(AXFDataModel*)dataModel
{
    if (_indexPath==nil)
    {
        //新增
//        [_deliverAddressArray addObject:dataModel];
        [[AXFLocationManager sharedInstance].dataArray addObject:dataModel];
    }
    else
    {
        //修改
        [[AXFLocationManager sharedInstance].dataArray removeObjectAtIndex:_indexPath.row];
        [[AXFLocationManager sharedInstance].dataArray insertObject:dataModel atIndex:_indexPath.row];
//        [_deliverAddressArray removeObjectAtIndex:_indexPath.row];
//        [_deliverAddressArray insertObject:dataModel atIndex:_indexPath.row];
        
    }
    [self loadData];
//    [self.tableView reloadData];
}

-(void)deleteAddressData
{
//    [_deliverAddressArray removeObjectAtIndex:_indexPath.row];
//    [self.tableView reloadData];
    [[AXFLocationManager sharedInstance].dataArray removeObjectAtIndex:_indexPath.row];
    [self loadData];
}

-(void)handleDeliverAddressArray
{
    
}

-(void)loadData
{
//    [_ModelMgr myAdress:^(NSArray<AXFDataModel *> * _Nonnull myAdressModels) {
//        _deliverAddressArray=myAdressModels.mutableCopy;
//        [self.tableView reloadData];
//    }];
    
//    
    _deliverAddressArray=[AXFLocationManager sharedInstance].dataArray.copy;
    [self.tableView reloadData];
}


#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _deliverAddressArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AXFSelectDeliverAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:SelectDeliverAddressCellID forIndexPath:indexPath];
    AXFDataModel*model=_deliverAddressArray[indexPath.row];
    cell.dataModel=model;
    __weak typeof(self)weakSelf=self;
    cell.clickEditBlock=^(){
        __strong typeof (self)strongSelf=weakSelf;
        [strongSelf clickedEditAddressButton:indexPath];
    };
    return cell;
}

-(void)dealloc
{
    NSLog(@"test");
}

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //传递选择数据
    if(self.selectAddressBlock)
    {
        self.selectAddressBlock(_deliverAddressArray[indexPath.row]);
        [self.navigationController popViewControllerAnimated:YES];
    }
    else
    {
        NSLog(@"CheckCallbackBlock");
    }
    

}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
